<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\Playlet;
use App\Model\PlayletBlocks;
use App\Model\PlayletBlocksRanking;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\PlayletBlocksRankingService;
use Hyperf\HttpServer\Contract\RequestInterface;

#[AutoController]
class PlayletBlocksRankingController  extends BaseController
{
    #[Inject]
    private PlayletBlocksRankingService $playletBlocksRankingService;
    /**
     * 小程序配置
     */
    public function Index(RequestInterface $request)
    {
        $data = $this->playletBlocksRankingService->search($request->query());
        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);

    }

    /**
     * 小程序配置创建
     */
    public function Create(RequestInterface $request)
    {
        $result = $this->playletBlocksRankingService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }

    }
    /**
     * 小程序配置删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletBlocksInfo = PlayletBlocksRanking::query()->where('id', $id)->where(['deleted' => 0])->first();
            if (empty($playletBlocksInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序配置不存在');
            }
            $playletBlocksInfo->deleted = 1;
            $playletBlocksInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 小程序配置创建
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $playletBlocksRankingInfo = PlayletBlocksRanking::query()->select(['id','name','playlet_ids'])->where('id', $id)->first();

            if(empty($playletBlocksRankingInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序前端配置不存在');
            }
            if($request->getMethod() == 'GET'){

                $playletIdsArr = explode(',',$playletBlocksRankingInfo->playlet_ids);

                $playletInfo = Playlet::query()->whereIn('id',$playletIdsArr)->select(['id','name','cover'])->get()->toArray();

                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$this->playletBlocksRankingService->combination($playletIdsArr,$playletInfo));

            }else{
                $result = $this->playletBlocksRankingService->createOrUpdate($request->post(),true,$playletBlocksRankingInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }
            }
        }
    }
}